<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider/>
    <el-steps :active="0" align-center finish-status="success">
      <el-step title="步骤1" description="选择商品类别"></el-step>
      <el-step title="步骤2" description="填写商品的基本信息"></el-step>
      <el-step title="步骤3" description="选择商品相册"></el-step>
    </el-steps>
    <el-divider/>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品类别" prop="categoryId">
        <el-input v-model="ruleForm.categoryId" placeholder="请输入商品类别的ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        categoryId: '25',
        categoryName:''
      },
      // 验证规则
      rules: {
        categoryId: [
          {required: true, message: '请输入商品类别的ID', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      // 执行全部的检查规则
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/categories/' + this.ruleForm.categoryId;
          console.log('url = ' + url)
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state === 2000) {
              let category = responseBody.data;
              if (category.isParent === 1) {
                this.$message.error('错误:选择的类别必须不包含子级类别!');
              }else {
                this.ruleForm.categoryName = category.name;
                localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
                this.$router.push('/sys-admin/product/spu/add-new-2');
              }
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>
<style>

</style>